package com.yxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yxy.constant.StatusConstant;
import com.yxy.domain.dto.FeedBackDTO;
import com.yxy.domain.po.Sign;
import com.yxy.domain.po.Sports;
import com.yxy.domain.vo.SignVO;
import com.yxy.domain.vo.SportVO;
import com.yxy.mapper.SignMapper;
import com.yxy.mapper.SportMapper;
import com.yxy.service.SignService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;


@Service
public class SignServiceImpl implements SignService {

    private final SignMapper signMapper;
    private final SportMapper sportMapper;

    public SignServiceImpl(SignMapper signMapper, SportMapper sportMapper) {
        this.signMapper = signMapper;
        this.sportMapper = sportMapper;
    }

    /**
     * 验证签到
     * @return
     */
    @Override
    public Boolean verifySign(Integer id) {
        // 查询当天签到状态
        LocalDate now = LocalDate.now();
        Sign sign = signMapper.getSignByUserIdAndCurrentDate(id, now);
        return sign != null && sign.getStatus() == StatusConstant.ENABLE;
    }

    /**
     * 打卡
     * @param id
     */
    @Override
    public void sign(Integer id) {
        LocalDate now = LocalDate.now();
        Sign sign = Sign.of(0, id, now, StatusConstant.ENABLE);
        signMapper.add(sign);
    }

    /**
     * 获取签到列表
     * @param id
     * @return
     */
    @Override
    public List<SignVO> list(Integer id) {
        List<Sign> list = signMapper.selectByUserId(id);
        List<SignVO> voList = BeanUtil.copyToList(list, SignVO.class);
        voList.forEach(item -> {
            // 根据用户id和打卡时间查询用户运动信息
            List<Sports> timeList = sportMapper.selectDurationByUserIdAndSignDate(id, item.getSignDate());
            for (Sports sports : timeList) {
                item.setSportId(sports.getId());
                item.setDuration(sports.getHours() * 60 + sports.getMinutes() + sports.getSeconds() / 60.0);
            }
        });
        return voList;
    }


}
